const userService = require('../../services/userService');
var app = getApp();
Component({
    properties: {
        showDefaultBanner:{
            type:Boolean,
            value:false
        },
        autoplay:{
            type:Boolean,
            value:true
        },
        indicator_dots:{
            type:Boolean,
            value:false
        },
        width:{
            type:Number
        },
        identification: {
            type: String
        },
        type: {
            type: Number,
            value:1 //1平台，2店铺
        },
        mid: {
            type: Number,
            value:0 //type为1是，mid为0，type为2时为具体mid
        }
    },

    data:{
        assets:app.assets,
        assetsByEnv:app.assetsByEnv,
        carousel:[],
        //存储swiper中最小高度的图片
        swiperHeight:0
    },

    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function () {
        },
        hide: function () { },
        resize: function () { },
    },
    ready:function(){
        this.getData();
    },
    methods: {
        imageLoad: function(e) {
            //获取图片真实宽度
            var $width=e.detail.width,
                $height=e.detail.height;
            //图片的真实宽高比例
            var ratio=$width/$height;
            //实际视图宽度、高度
            var viewWidth=this.data.width,
                viewHeight=viewWidth/ratio;
            //取轮播图片最小高度
            if(this.data.swiperHeight == 0 || this.data.swiperHeight > viewHeight) {
                this.setData({
                    swiperHeight: viewHeight
                });
            }
        },
        getData: function () {
            // 轮播图
            var user = wx.getStorageSync('user');
            var params = {};
            params.project_type = 4;
            params.openid = user.openid;
            params.identification = this.data.identification;
            params.type = this.data.type;
            params.mid = this.data.mid;
            if(user){
                userService.getAdvertiseList(params).then((data=>{
                    if(data.code == 1) {
                        this.setData({
                            carousel:data.data
                        })
                        this.triggerEvent('success',{carousel:data.data});
                    }else{
                        wx.showToast({
                            title:data.msg,
                            icon:'none'
                        });
                        this.triggerEvent('success',{carousel:[]});
                    }
                }),(error=>{
                    this.showToast('获取广告列表接口异常');
                    this.triggerEvent('success',{carousel:[]});
                }));
            }
        },
        _saveAdClicks:function(user,prage){
            if(user){
                userService.saveAdClicks(prage).then((data=>{
                    if(data.code != 1) {
                        wx.showToast({
                            title:data.msg,
                            icon:'none'
                        });
                    }
                }),(error=>{
                    wx.showToast({
                        title: 'saveAdClicks'+error.message,
                        icon:'none'
                    });
                }));
            } else{
                wx.showToast({
                    title: '未获取到用户openid',
                    icon:'none'
                });
            }
        },
        _navigateForm:function(item){
            if (item.url_ids == 1 && item.form_id == 0) {//尾货表单
                wx.navigateTo({
                    url: '/pages/cargoadd/cargoadd?msg_id='+item.id+'&source_type=2'
                })
            } else if (item.url_ids == 1 && item.form_id == 1) {//尾货详情
                wx.navigateTo({
                    url: '/pages/cargosee/cargosee?msg_id='+item.id+'&source_type=2'
                })
            } else if (item.url_ids == 2 && item.form_id == 0) {//彩票表单
                wx.navigateTo({
                    url: '/pages/yiyeorderadd/yiyeorderadd?msg_id='+item.id+'&source_type=2'
                })
            } else if (item.url_ids == 2 && item.form_id == 1) {//彩票详情
                wx.navigateTo({
                    url: '/pages/yiyeorderadd/yiyeorderadd?msg_id='+item.id+'&source_type=2'
                })
            } else{
                wx.showToast({
                    title:'未知表单链接类型',
                    icon:'none'
                });
            }
        },
        _gotoProduct:function(item){
            var url_idsstring = item.url_ids;
            var url_ids = url_idsstring.split(',');
            if (url_ids.length>1) {
                wx.navigateTo({
                    url: '/pages/searchList/searchList?ids='+url_idsstring
                })
            } else if (url_idsstring==0) {
                wx.navigateTo({
                    url: '/pages/searchList/searchList?goFirst=1'//进搜索页显示所有商品
                })
            } else if (url_ids.length==1 && url_idsstring!=0) {
                wx.navigateTo({
                    url: '/pages/productDetail/productDetail?id='+url_idsstring
                })
            } else{
                wx.showToast({
                    title:'未知商品链接类型',
                    icon:'none'
                });
            }
        },
        goPoster:function(e){
            var item = e.currentTarget.dataset.item;
            var user = wx.getStorageSync('user');
            var prage = {};
            prage.project_type = 4;
            prage.openid = user.openid;
            prage.id = item.id;
            //统计点击次数，无链接情况不统计
            if (item.url_type != 1) {
                this._saveAdClicks(user,prage);
            }
            //商品类型
            if(item.url_type == 2) {
                this._gotoProduct(item);
            //优惠券
            } else if(item.url_type == 4) {
                wx.navigateTo({
                    url: '../couponsCenter/couponsCenter?ids='+item.url_ids
                })
            //表单
            }else if(item.url_type == 5) {
                this._navigateForm(item);
            //消息
            }else if(item.url_type == 6) {
                wx.navigateTo({
                    url: '../message/message?msg_id='+item.url_ids+'&type=1'
                })
            }
        },
    }
});